package cn.lg.soar.system.biz.modules.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.DisabledTenantAccess;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.auth.entity.Component;
import cn.lg.soar.system.biz.modules.auth.model.ComponentBatchCreateVO;
import cn.lg.soar.system.biz.modules.auth.model.ComponentVO;
import cn.lg.soar.system.biz.modules.auth.service.ComponentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 组件
 * @author luguoxiang
 * @since 2025-05-03
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Tag(name = "组件")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/component")
public class ComponentController extends BaseController<Component, ComponentService> {

    @Override
    protected Serializable getId(Component entity) {
        return entity.getId();
    }

    @DisabledTenantAccess
    @Log(value = "新增组件", type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @Operation(summary = "新增")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated(Create.class) Component data) {
        return super.createResult(data);
    }

    @DisabledTenantAccess
    @Log(value = "修改组件", type = LogTypeEnum.UPDATE)
    @Operation(summary = "修改")
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated(Update.class) Component data) {
        return super.updateResult(data);
    }

    @DisabledTenantAccess
    @Log(value = "删除组件", type = LogTypeEnum.REMOVE)
    @Operation(summary = "删除", description = "根据 id 删除")
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DisabledTenantAccess
    @Operation(summary = "详情", description = "根据 ID 获取一条数据")
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<ComponentVO> detail(@RequestParam(name = "id") Long id) {
        return HttpResult.data(service.detail(id));
    }

    @Operation(summary = "查询", description = "多功能查询（分页、列表、统计）")
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<Component>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @DisabledTenantAccess
    @Log(value = "导出组件", type = LogTypeEnum.EXPORT)
    @Operation(summary = "导出")
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(@RequestParam(name = "filename", required = false) String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @DisabledTenantAccess
    @Log(value = "导入组件", type = LogTypeEnum.IMPORT)
    @Operation(summary = "导入")
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @DisabledTenantAccess
    @Log(value = "批量新增", type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @Operation(summary = "批量新增")
    @RequestMapping(path = "batchCreate", method = RequestMethod.POST)
    public HttpResult<Void> batchCreate(@RequestBody List<ComponentBatchCreateVO> data) throws ExecutionException, InterruptedException {
        service.batchCreate(data);
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @Log(value = "清空缓存")
    @Operation(summary = "清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }

}